package br.unifor.cadeventos.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;

import br.unifor.cadeventos.internacionalizacao.Messagens;
import br.unifor.cadeventos.utils.DesktopContext;

public class FormEventos extends JPanel {

	private static final long serialVersionUID = 6373088238254085307L;

	/**
	 * Components
	 * */
	private JTextField textField;
	private JTextField textField_1;
	private JLabel lblTtulo;
	private JLabel lblLocal;
	private JFormattedTextField formattedTextField;
	private JFormattedTextField formattedTextField_1;
	private JFormattedTextField formattedTextField_2;
	private JFormattedTextField formattedTextField_3;
	private JTextArea textArea;

	/**
	 * Create the panel.
	 */
	public FormEventos() {
		setLayout(null);

		DesktopContext.getContextFrame().setTitle("Cadastro de Eventos");

		lblTtulo = new JLabel("Título*:");
		lblTtulo.setBounds(33, 51, 46, 14);
		add(lblTtulo);

		textField = new JTextField();
		textField.setBounds(73, 48, 548, 20);
		add(textField);
		textField.setColumns(10);

		lblLocal = new JLabel("Local*:");
		lblLocal.setBounds(33, 79, 46, 14);
		add(lblLocal);

		textField_1 = new JTextField();
		textField_1.setBounds(73, 76, 548, 20);
		add(textField_1);
		textField_1.setColumns(10);

		JButton btnSalvar = new JButton("Salvar");
		btnSalvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (verificaCamposObrigatorios()) {
					
				} else {
					JOptionPane.showMessageDialog(null, Messagens.MSG_CMP_OBR);
				}
			}
		});
		btnSalvar.setBounds(238, 300, 89, 23);
		add(btnSalvar);

		JLabel lblDataInicio = new JLabel("Data Inicio*:");
		lblDataInicio.setBounds(33, 104, 76, 16);
		add(lblDataInicio);

		try {
			formattedTextField = new JFormattedTextField(new MaskFormatter("##/##/####"));
			formattedTextField.setBounds(106, 101, 76, 22);
			add(formattedTextField);
		

			JLabel lblHoraInicio = new JLabel("Hora Inicio*:");
			lblHoraInicio.setBounds(193, 104, 76, 16);
			add(lblHoraInicio);
	
			formattedTextField_1 = new JFormattedTextField(new MaskFormatter("##:##"));
			formattedTextField_1.setBounds(262, 101, 48, 22);
			add(formattedTextField_1);
	
			JLabel lblDataFim = new JLabel("Data Fim*:");
			lblDataFim.setBounds(355, 104, 74, 16);
			add(lblDataFim);
	
			formattedTextField_2 = new JFormattedTextField(new MaskFormatter("##/##/####"));
			formattedTextField_2.setBounds(417, 101, 76, 22);
			add(formattedTextField_2);
	
			JLabel lblHoraFim = new JLabel("Hora Fim*:");
			lblHoraFim.setBounds(503, 104, 67, 16);
			add(lblHoraFim);
	
			formattedTextField_3 = new JFormattedTextField(new MaskFormatter("##:##"));
			formattedTextField_3.setBounds(575, 101, 46, 22);
			add(formattedTextField_3);
		
		} catch (ParseException e) {
			e.printStackTrace();
		}

		JLabel lblDescrio = new JLabel("Descrição:");
		lblDescrio.setBounds(33, 167, 76, 16);
		add(lblDescrio);
		JScrollPane scroll = new JScrollPane();
		scroll.setBounds(121, 162, 500, 93);
		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		add(scroll);

		textArea = new JTextArea();
		scroll.setViewportView(textArea);

		JButton btnNewButton = new JButton("Cancelar");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				DesktopContext.getContextFrame().setContentPane(
						new FiltroEvento());
				DesktopContext.getContextFrame().setVisible(true);
			}
		});
		btnNewButton.setBounds(358, 300, 89, 23);
		add(btnNewButton);
		textArea.setVisible(true);

		setVisible(true);

	}
	
	private boolean verificaCamposObrigatorios() {
		if (!textField.getText().trim().equals("")
				|| !textField_1.getText().trim().equals("")
				|| validaData(formattedTextField)
				|| validaHora(formattedTextField_1)
				|| validaData(formattedTextField_2)
				|| validaHora(formattedTextField_3)) {
			return false;
		} else {
			return true;
		}
	}
	
	private boolean validaData(JFormattedTextField fieldDate) {
		return true;
	}
	
	private boolean validaHora(JFormattedTextField fieldDate) {
		return true;
	}
}
